// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import FastClick from 'fastclick'
import VueRouter from 'vue-router'
import App from './App'
import router from './router'
import axios from 'axios'
import VueAxios from 'vue-axios'
import {AlertPlugin, ToastPlugin, LoadingPlugin, Previewer, ConfirmPlugin} from 'vux'
import store from './store'
import ActionCable from 'actioncable'
import {current_user} from './util.js'

const cable = ActionCable.createConsumer("wss://api.chuanggj.com/websocket")
Vue.use(VueRouter, VueAxios, axios)
Vue.use(AlertPlugin)
Vue.use(ToastPlugin)
Vue.use(LoadingPlugin)
Vue.use(ConfirmPlugin)
Vue.use(Previewer)
Vue.config.productionTip = false
Vue.prototype.$cable = cable
Vue.prototype.$current_user = current_user()
var token = localStorage.getItem("chuang_token")
let axios_instance = axios.create({
    //baseURL: "http://127.0.0.1:3000/web/",
    baseURL: "https://api.chuanggj.com/web/",
    headers: {
        'Authorization': localStorage.getItem("chuang_token")
    },
    responseType: 'json',
    timeout: 5000,
    transformResponse: [function (data) {
        return data;
    }],
})
Vue.prototype.$http = axios_instance
FastClick.attach(document.body)
axios_instance.interceptors.request.use(config => {
    return config
}, err => {
    //return Promise.reject(err)
})
axios_instance.interceptors.response.use(res => {
    let code = res.data.code
    if (code && code == 419) {
        localStorage.removeItem("chuang_token")
        window.location.href="/#/login"
    } else if (code && code > 200) {
        Vue.$vux.toast.show({
            type: "cancel",
            text: res.data.msg
        })
        return 
    }
    return res
}, err => {
    Vue.$vux.toast.show({
        type: "cancel",
        text: "网络连接失败"
    })
    //return Promise.reject(err)
})
/* eslint-disable no-new */
new Vue({
    router,
    store,
    render: h => h(App)
}).$mount('#app-box')
